package com.dgut.service.serviceImpl;

import com.dgut.dao.AreaDao;
import com.dgut.pojo.Area;
import com.dgut.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "areaCache")
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaDao areaDao;

    @Override
    @Cacheable(key = "#root.args[0]")
    public List<Area> findRiskArea(int grade) {
        return areaDao.findByGrade(grade);
    }

    @Override
    @CacheEvict(value = "areaCache",allEntries = true)
    public void saveArea(Area area) {
        areaDao.save(area);
    }

    @Override
    public void deleteArea(Area area) {
        areaDao.deleteById(area.getId());
    }

    @Override
    @Cacheable(key = "#root.args[0]")
    public Area findAreaByName(String name) {
        return areaDao.findAreaByName(name);
    }
}
